package cn.crm.web.controller.business;

import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.domain.entity.SysUser;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.common.utils.poi.ExcelUtil;
import cn.crm.system.domain.pojo.dto.BusinessDto;
import cn.crm.system.domain.pojo.dto.SeriesDto;
import cn.crm.system.domain.pojo.dto.SpecificationDto;
import cn.crm.system.domain.pojo.entity.*;
import cn.crm.system.domain.pojo.vo.BusinessVo;
import cn.crm.system.domain.pojo.vo.SeriesAndspecificationVo;
import cn.crm.system.domain.pojo.vo.SpecificationVo;
import cn.crm.system.service.ISpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static cn.crm.common.core.domain.AjaxResult.success;
import static cn.crm.common.utils.SecurityUtils.getUsername;

@Slf4j
@RestController
@RequestMapping("/specification")
@Api(tags = "业务单头参数模块")
public class SpecificationController  extends BaseController {
    @Autowired
    private ISpecificationService specificationService;

    //下载模板 灯具
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Specificationes> util = new ExcelUtil<Specificationes>(Specificationes.class);
        util.importTemplateExcel(response, "参数数据");
    }

    //下载模板 燈帶及硬燈條
    @PostMapping("/importTemplates")
    public void importTemplates(HttpServletResponse response)
    {
        ExcelUtil<Specifications> util = new ExcelUtil<Specifications>(Specifications.class);
        util.importTemplateExcel(response, "参数数据");
    }

    //下载模板 電源驅動
    @PostMapping("/importTemplatess")
    public void importTemplatess(HttpServletResponse response)
    {
        ExcelUtil<Specificationss> util = new ExcelUtil<Specificationss>(Specificationss.class);
        util.importTemplateExcel(response, "参数数据");
    }
    //下载模板 軌道/配件/鋁材/膠條
    @PostMapping("/importTemplatesss")
    public void importTemplatesss(HttpServletResponse response)
    {
        ExcelUtil<Specificationsss> util = new ExcelUtil<Specificationsss>(Specificationsss.class);
        util.importTemplateExcel(response, "参数数据");
    }

    //上传
    @Log(title = "产品参数管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:prodect:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,
                                 @RequestParam(value = "prodectId", required = false) Long prodectId) throws Exception
    {
        ExcelUtil<Specification> util = new ExcelUtil<Specification>(Specification.class);
        List<Specification> SpecificationList = util.importExcel(file.getInputStream());
        // 获取文件名
        String fileName = file.getOriginalFilename();
        String prefix = fileName.substring(0, fileName.indexOf('_'));

        // 定义允许的前缀列表
        List<String> allowedPrefixes = Arrays.asList("燈具", "燈帶及硬燈條", "電源驅動", "配件及型材");
        String message =null;
// 检查 prefix 是否在允许的前缀列表中
        if (!allowedPrefixes.contains(prefix)) {
            return error("文件名不对，前缀必须是：燈具, 燈帶及硬燈條, 電源驅動, 配件及型材 中的一种");
        }else {

            String operName = getUsername();
           message = specificationService.importSpecification(SpecificationList, updateSupport, operName, prodectId,prefix);
        }
        return success(message);
    }


    @GetMapping("/list")
    @ApiOperation("查询表身资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:list')")
    @Log(title = "产品参数管理", businessType = BusinessType.OTHER)
    public TableDataInfo Getspecificationlist(SpecificationDto SpecificationDto) {
        log.debug("开始处理查询表身资料的接口");
        startPage();
        List<SpecificationVo> specificationVos = specificationService.selectSpecificationList(SpecificationDto);
        return  getDataTable(specificationVos);
    }

    @PreAuthorize("@ss.hasPermi('system:prodect:add')")
    @PostMapping("/add")
    @ApiOperation("添加表身资料")
    @Log(title = "产品参数管理", businessType = BusinessType.INSERT)
    public AjaxResult  addBusiness(@RequestBody SpecificationDto SpecificationDto) {
        log.debug("开始处理添加表身资料的接口");
        int i = specificationService.insertSpecification(SpecificationDto);
        if (i < 1){
            return AjaxResult.error("添加参数失败");
        }
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询参数资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:query')")
    @Log(title = "产品参数管理", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable Long id){
        log.debug("开始通过id查询客户资料的接口");
        SpecificationVo specificationVo = specificationService.selectSpecificationByid(id);
        return  success(specificationVo);
    }
    @Log(title = "产品参数管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改表身资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:update')")
    public AjaxResult update( @RequestBody SpecificationDto SpecificationDto){
        log.debug("开始修改表身参数资料的接口");
        int updatespecification = specificationService.updatespecification(SpecificationDto);
        if (updatespecification < 1){
            return AjaxResult.error("修改表身参数失败");
        }
        return AjaxResult.success("修改参数资料成功");
    }

    @Log(title = "产品参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{specificationId}")
    @ApiOperation("删除表身资料")
    @PreAuthorize("@ss.hasPermi('system:prodect:delete')")
    public AjaxResult delete(@PathVariable Long[] id,
                @PathVariable Long[] specificationId
    ){
        log.debug("开始删除表身资料的接口");
        int deletespecification = specificationService.deletespecification(id,specificationId);
        if (deletespecification < 1){
            return AjaxResult.error("删除客户资料失败");
        }

        return AjaxResult.success("删除客户资料成功");
    }


    @Log(title = "表头管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:prodect:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpecificationDto SpecificationDto)
    {
        List<SpecificationVo> specificationVos = specificationService.selectSpecificationList(SpecificationDto);
        ExcelUtil<SpecificationVo> util = new ExcelUtil<SpecificationVo>(SpecificationVo.class);
        util.exportExcel(response, specificationVos, "表身数据");
    }

}
